package com.example.booleanfilter.config;

import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.URISyntaxException;


@Configuration
public class RedisSesionConfig {

    @Value("${spring.redis.cluster.nodes}")
    String add;
    @Value("${spring.redis.password}")
    String pass;

    public Config config() throws URISyntaxException {
        System.out.println("ok");
        System.out.println(add);
        System.out.println(pass);
        Config configs = new Config();
        String[] adds = add.split(",");
        for (int i = 0; i < adds.length; i++) {
            adds[i] = "redis://" + adds[i];
        }
        //#连接间隔 心跳 客户端长时间未使用，服务端会断开
        configs.useClusterServers().addNodeAddress(adds).setClientName("rediss").setPassword(pass).setMasterConnectionPoolSize(40);
        return configs;
    }

    @Bean
    public RedissonClient redissonClient() throws URISyntaxException {

        RedissonClient redissonClient = Redisson.create(config());

        return redissonClient;
    }

    @Bean
    public RLock rLock() throws URISyntaxException {

        return redissonClient().getLock("kk");
    }
}